define(function(){
	var hjs = function(){
		//轮播图开始
		function change(num){
			var $wrap = $('#lbtwrap');
			var $middle = $('#lbtwrap #middle');
	//		var $imgs = $('#lbtwrap img');
			var $li = $('#lbtwrap li');
			$($li[0]).css("background","white");
			// 设置卷轴的宽度
			var $w = $wrap.width()*num;
			$middle.width($w);
			// 设置跟随变量，记录图片的下标
			var $index = 0;
			// 图片自动轮播到下一张
			function nextImg(){
				$index++;
				if($index>num-1){
					$index = 0;
				}
				var $left = $index*$wrap.width()*-1;
				// $middle[0].style.left = $left + 'px';
				$middle.css('left',$left);
				for(var i=0;i<$li.length;i++){
					$($li[i]).css("background","#ccc");
				}
				$($li[$index]).css("background","white");
			}

			var timer = setInterval(nextImg,3000);

			// 在打断轮播的时候清除定时器
			function clearTimer(){
				clearInterval(timer);
				timer = setInterval(nextImg,3000);
			}

			//向左滑动跳到下一张图片,向右滑动跳到上一张图片
			var startX,endX;
			$wrap[0].addEventListener('touchstart',function(e){
				var touch = e.targetTouches[0];
				startX = touch.pageX;
			});
			$wrap[0].addEventListener('touchmove',function(e){
				var touch = e.targetTouches[0];
				endX = touch.pageX;
			});
			$wrap[0].addEventListener('touchend',function(e){
				if(endX-startX>100){
					$index--;
					if($index<0){
						$index = num-1;
					}
					var $left = $index*$wrap.width()*-1;
					$middle.css('left',$left);
					for(var i=0;i<$li.length;i++){
						$($li[i]).css("background","#ccc");
					}
					$($li[$index]).css("background","white");
					clearTimer();
				}else if(endX-startX<-100){
					nextImg();
					clearTimer();
				}
			})

			// 点击对应的按钮轮播到对应的图片
			for(var i=0;i<$li.length;i++){
				// 闭包实现
				(function(nums){
					$($li[nums]).click(function(){
						$index = nums;
						var $left = nums*$wrap.width()*-1;
						$middle.css('left',$left);
						clearTimer();
					})
				})(i);
			}
		}//轮播图结束

		change(2);
	}
	return {
		hjs:hjs
	}
})
